>enqueue(); } ); $endpoint = $c->get( 'order-tracking.endpoint.controller' ); assert( $endpoint instanceof OrderTrackingEndpoint ); $logger = $c->get( 'woocommerce.logger.woocommerce' ); assert( $logger instanceof LoggerInterface ); add_action( 'woocommerce_gzd_shipment_status_shipped', static function( int $shipment_id, Shipment $shipment ) use ( $endpoint, $logger ) { if ( ! apply_filters( 'woocommerce_paypal_payments_sync_gzd_tracking', true ) ) { return; } $wc_order = $shipment->get_order(); if ( ! is_a( $wc_order, WC_Order::class ) ) { return; } $transaction_id = $wc_order->get_transaction_id(); if ( empty( $transaction_id ) ) { return; } $tracking_data = array( 'transaction_id' => $transaction_id, 'status' => 'SHIPPED', ); $provider = $shipment->get_shipping_provider(); if ( ! empty( $provider ) && $provider !== 'none' ) { /** * The filter allowing to change the default Germanized carrier for order tracking, * such as DHL_DEUTSCHE_POST, DPD_DE, ... */ $tracking_data['carrier'] = (string) apply_filters( 'woocommerce_paypal_payments_default_gzd_carrier', 'DHL_DEUTSCHE_POST', $provider ); } try { $tracking_information = $endpoint->get_tracking_information( $wc_order->get_id() ); $tracking_data['tracking_number'] = $tracking_information['tracking_number'] ?? ''; if ( $shipment->get_tracking_id() ) { $tracking_data['tracking_number'] = $shipment->get_tracking_id(); } ! $tracking_information ? $endpoint->add_tracking_information( $tracking_data, $wc_order->get_id() ) : $endpoint->update_tracking_information( $tracking_data, $wc_order->get_id() ); } catch ( Exception $exception ) { $logger->error( "Couldn't sync tracking information: " . $exception->getMessage() ); } }, 500, 2 ); } /** * Migrates the old Pay Later button and messaging settings for new Pay Later Tab. * * The migration will be done on plugin upgrade if it hasn't already done. * * @param ContainerInterface $c The Container. * @throws NotFoundException When setting was not found. */ protected function migrate_pay_later_settings( ContainerInterface $c ): void { $is_pay_later_settings_migrated_option_name = 'woocommerce_ppcp-is_pay_later_settings_migrated'; $is_pay_later_settings_migrated = get_option( $is_pay_later_settings_migrated_option_name ); if ( $is_pay_later_settings_migrated ) { return; } add_action( 'woocommerce_paypal_payments_gateway_migrate_on_update', function () use ( $c, $is_pay_later_settings_migrated_option_name ) { $settings = $c->get( 'wcgateway.settings' ); assert( $settings instanceof Settings ); $disable_funding = $settings->has( 'disable_funding' ) ? $settings->get( 'disable_funding' ) : array(); $available_messaging_locations = array_keys( $c->get( 'wcgateway.settings.pay-later.messaging-locations' ) ); $available_button_locations = array_keys( $c->get( 'wcgateway.button.locations' ) ); if ( in_array( 'credit', $disable_funding, true ) ) { $settings->set( 'pay_later_button_enabled', false ); } else { $settings->set( 'pay_later_button_enabled', true ); $selected_button_locations = $this->selected_locations( $settings, $available_button_locations, 'button' ); if ( ! empty( $selected_button_locations ) ) { $settings->set( 'pay_later_button_locations', $selected_button_locations ); } } $selected_messaging_locations = $this->selected_locations( $settings, $available_messaging_locations, 'message' ); if ( ! empty( $selected_messaging_locations ) ) { $settings->set( 'pay_later_messaging_enabled', true ); $settings->set( 'pay_later_messaging_locations', $selected_messaging_locations ); $settings->set( 'pay_later_enable_styling_per_messaging_location', true ); foreach ( $selected_messaging_locations as $location ) { $this->migrate_message_styling_settings_by_location( $settings, $location ); } } else { $settings->set( 'pay_later_messaging_enabled', false ); } $settings->persist(); update_option( $is_pay_later_settings_migrated_option_name, true ); } ); } /** * Migrates the messages styling setting by given location. * * @param Settings $settings The settings. * @param string $location The location. * @throws NotFoundException When setting was not found. */ protected function migrate_message_styling_settings_by_location( Settings $settings, string $location ): void { $old_location = $location === 'checkout' ? '' : "_{$location}"; $layout = $settings->has( "message{$old_location}_layout" ) ? $settings->get( "message{$old_location}_layout" ) : 'text'; $logo_type = $settings->has( "message{$old_location}_logo" ) ? $settings->get( "message{$old_location}_logo" ) : 'primary'; $logo_position = $settings->has( "message{$old_location}_position" ) ? $settings->get( "message{$old_location}_position" ) : 'left'; $text_color = $settings->has( "message{$old_location}_color" ) ? $settings->get( "message{$old_location}_color" ) : 'black'; $style_color = $settings->has( "message{$old_location}_flex_color" ) ? $settings->get( "message{$old_location}_flex_color" ) : 'blue'; $ratio = $settings->has( "message{$old_location}_flex_ratio" ) ? $settings->get( "message{$old_location}_flex_ratio" ) : '1x1'; $settings->set( "pay_later_{$location}_message_layout", $layout ); $settings->set( "pay_later_{$location}_message_logo", $logo_type ); $settings->set( "pay_later_{$location}_message_position", $logo_position ); $settings->set( "pay_later_{$location}_message_color", $text_color ); $settings->set( "pay_later_{$location}_message_flex_color", $style_color ); $settings->set( "pay_later_{$location}_message_flex_ratio", $ratio ); } /** * Finds from old settings the selected locations for given type. * * @param Settings $settings The settings. * @param string[] $all_locations The list of all available locations. * @param string $type The setting type: 'button' or 'message'. * @return string[] The list of locations, which should be selected. */ protected function selected_locations( Settings $settings, array $all_locations, string $type ): array { $button_locations = array(); foreach ( $all_locations as $location ) { $location_setting_name_part = $location === 'checkout' ? '' : "_{$location}"; $setting_name = "{$type}{$location_setting_name_part}_enabled"; if ( $settings->has( $setting_name ) && $settings->get( $setting_name ) ) { $button_locations[] = $location; } } return $button_locations; } /** * Migrates the old smart button settings. * * The migration will be done on plugin upgrade if it hasn't already done. * * @param ContainerInterface $c The Container. */ protected function migrate_smart_button_settings( ContainerInterface $c ): void { $is_smart_button_settings_migrated_option_name = 'woocommerce_ppcp-is_smart_button_settings_migrated'; $is_smart_button_settings_migrated = get_option( $is_smart_button_settings_migrated_option_name ); if ( $is_smart_button_settings_migrated ) { return; } add_action( 'woocommerce_paypal_payments_gateway_migrate_on_update', function () use ( $c, $is_smart_button_settings_migrated_option_name ) { $settings = $c->get( 'wcgateway.settings' ); assert( $settings instanceof Settings ); $available_button_locations = array_keys( $c->get( 'wcgateway.button.locations' ) ); $selected_button_locations = $this->selected_locations( $settings, $available_button_locations, 'button' ); if ( ! empty( $selected_button_locations ) ) { $settings->set( 'smart_button_locations', $selected_button_locations ); $settings->persist(); } update_option( $is_smart_button_settings_migrated_option_name, true ); } ); } /** * Changes the button rendering place for page builders * that do not work well with our default places. * * @return void */ protected function fix_page_builders(): void { add_action( 'init', function() { if ( $this->is_elementor_pro_active() || $this->is_divi_theme_active() ) { add_filter( 'woocommerce_paypal_payments_single_product_renderer_hook', function(): string { return 'woocommerce_after_add_to_cart_form'; }, 5 ); } } ); } /** * Checks whether the Elementor Pro plugins (allowing integrations with WC) is active. * * @return bool */ protected function is_elementor_pro_active(): bool { return is_plugin_active( 'elementor-pro/elementor-pro.php' ); } /** * Checks whether the Divi theme is currently used. * * @return bool */ protected function is_divi_theme_active(): bool { $theme = wp_get_theme(); return $theme->get( 'Name' ) === 'Divi'; } }